package com.das.mechanic_base.adapter.main;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.RecyclerView;

import com.das.mechanic_base.R;

import java.util.ArrayList;
import java.util.List;

public class X3CarSexAdapter extends RecyclerView.Adapter<X3CarSexAdapter.CarSexHolder> {

    private List<String> mList;

    private List<Boolean> sList;

    private Context mContext;

    public X3CarSexAdapter(Context mContext) {
        this.mList = new ArrayList<>();
        this.mContext = mContext;
        this.sList = new ArrayList<>();
    }

    public void changeSexAdapter(List<String> mList) {
        this.mList = mList;
        this.sList.clear();
        for (int i = 0; i < mList.size(); i++) {
            sList.add(i,i == 0);
        }
        if (iOnClickSex != null) {
            iOnClickSex.iOnClickSelectSex(mList.get(0));
        }
        notifyDataSetChanged();
    }

    public void changeSelectSex(String sex) {
        int index = mList.indexOf(sex);
        if (index == -1) {
            return;
        }
        this.sList.clear();
        for (int i = 0; i < mList.size(); i++) {
            sList.add(i,i == index);
        }
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public X3CarSexAdapter.CarSexHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new CarSexHolder(LayoutInflater.from(mContext).inflate(R.layout.x3_car_sex_layout,parent,false));
    }

    @Override
    public void onBindViewHolder(@NonNull X3CarSexAdapter.CarSexHolder holder, int position) {
        if (sList.get(position)) {
            holder.tv_sex.setBackground(ContextCompat.getDrawable(mContext,R.drawable.x3_car_sex_text_bg));
            holder.tv_sex.setTextColor(Color.parseColor("#0077ff"));
        }else {
            holder.tv_sex.setBackground(ContextCompat.getDrawable(mContext,R.drawable.x3_alone_point_bg));
            holder.tv_sex.setTextColor(Color.parseColor("#707380"));
        }
        holder.tv_sex.setText(mList.get(position) + "");
        holder.itemView.setOnClickListener(v -> {
            for (int i = 0; i < sList.size(); i++) {
                sList.remove(i);
                sList.add(i,i == position);
            }
            notifyDataSetChanged();
            if (iOnClickSex != null) {
                iOnClickSex.iOnClickSelectSex(mList.get(position));
            }
        });
    }

    IOnClickSex iOnClickSex;

    public void setiOnClickSex(IOnClickSex iOnClickSex) {
        this.iOnClickSex = iOnClickSex;
    }

    public interface IOnClickSex {
        void iOnClickSelectSex(String sex);
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    class CarSexHolder extends RecyclerView.ViewHolder {
        TextView tv_sex;
        public CarSexHolder(@NonNull View itemView) {
            super(itemView);
            tv_sex = itemView.findViewById(R.id.tv_sex);
        }
    }
}
